﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

namespace parkingclient
{
    public partial class reservation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] == null)
            {
                lblWelcome.Text = "[<a href='user_login.aspx'>Login</a>]   [<a href='registration.aspx'>Register</a>]";
            }
            else if (int.Parse(Session["userType"].ToString()) == 3) //for admin, add link to the admin dashboard
            {
                lblWelcome.Text = "Welcome " + Session["user"] + "   [<a href='admin_dashboard.aspx'>Admin Dashboard</a>]    [<a href='user_logout.aspx'>Logout</a>]";
            }
            else
            {
                lblWelcome.Text = "Welcome " + Session["user"] + "   [<a href='user_logout.aspx'>Logout</a>]";
            }
            
            //load the time
            LoadTimeDropDownList(ref ddlStartTime, 9, 15, 30);
            LoadTimeDropDownList(ref ddlEndTime, 10, 16, 30);
        }

        protected void txtGarage_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void LoadTimeDropDownList(ref DropDownList ddl, int startHour,
int endHour, int incrementInMinutes)
        {
            DateTime now = DateTime.Now;
            DateTime startTime = new DateTime(now.Year, now.Month, now.Day,
            startHour, 0, 0);
            DateTime endTime = new DateTime(now.Year, now.Month, now.Day,
            endHour, 0, 0);

            LoadTimeDropDownList(ref ddl, startTime, endTime,
            incrementInMinutes);
        }

        private void LoadTimeDropDownList(ref DropDownList ddl, DateTime startTime, DateTime endTime, int incrementInMinutes)
        {
            DateTime now = DateTime.Now;
            bool haveIndex = false;

            while (startTime <= endTime)
            {
                ddl.Items.Add(startTime.ToString("H:mm"));
                startTime = startTime.AddMinutes(incrementInMinutes);

                if ((startTime > now) && (!haveIndex))
                {
                    ddl.SelectedIndex = ddl.Items.Count - 1;
                    haveIndex = true;
                }
            }

        }

        protected void btnFindSlot_Click(object sender, EventArgs e)
        {
            ddlSpot.Items.Clear();

            string date = Calendar1.SelectedDate.ToString();
            string startTime = ddlStartTime.SelectedValue;
            string endTime = ddlEndTime.SelectedValue;
            int garage = Int32.Parse(txtGarage.SelectedValue);

            int userType = 0;
            ParkingService.WebService1 parking = new ParkingService.WebService1();

            //get the user type
            if (Session["user"] != null)
                userType = parking.getAccountTypeByEmail(Session["user"].ToString());

            //get the available spot
            string spotRes = parking.getAvailableSpotByUserType(startTime, endTime, garage, userType);

            lblSelectSpot.Visible = true;
            ddlSpot.Visible = true;
            btnReserve.Visible = true;

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(spotRes);

            XmlNodeList floors = xmlDoc.GetElementsByTagName("floor");
            
            foreach(XmlNode f in floors)
            {
                XmlNodeList spots = f.ChildNodes;
                foreach (XmlNode s in spots)
                {
                    ddlSpot.Items.Add(new ListItem("Floor " + f.Attributes["number"].Value + " - " + s.Attributes["id"].Value , s.Attributes["id"].Value));
                }
            }



        }

        protected void btnReserve_Click(object sender, EventArgs e)
        {
            ParkingService.WebService1 parking = new ParkingService.WebService1();
            int resID = parking.reserveSpot(Session["user"].ToString(), ddlStartTime.SelectedValue, ddlEndTime.SelectedValue, Int32.Parse(ddlSpot.SelectedValue));

            Response.Redirect("confirmation.aspx?resid=" + resID);
        }

        protected void ImageMap1_Click(object sender, ImageMapEventArgs e)
        {

        }

    }
}